package org.lanqiao.util;

import java.util.ArrayList;
import java.util.List;

public class Pager<T> {
    //当前页
    private int currPageNo = 1;
    //每页数据条数
    private int pageSize = 3;
    //总记录数
    private int totalCount;
    //总页数
    private int totalPageCount;
    //当前页的记录
    private List<T> list = new ArrayList<>();

    public int getCurrPageNo() {
        return currPageNo;
    }

    // 设置当前页
    public void setCurrPageNo(int currPageNo) {
        if (currPageNo <= 1) {
            this.currPageNo = 1;
        } else if (currPageNo >= this.totalPageCount) {
            this.currPageNo = this.totalPageCount;
        } else {
            this.currPageNo = currPageNo;
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    // 设置总记录数
    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            // 设置总页数
            this.totalPageCount = this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize
                    : this.totalCount / this.pageSize + 1;
        }

    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    // 获取记录列表
    public List<T> getList() {
        if (this.currPageNo == this.totalPageCount) {
            return this.list.subList((this.currPageNo - 1) * this.pageSize, this.list.size());
        } else {
            return this.list.subList((this.currPageNo - 1) * this.pageSize, this.currPageNo * this.pageSize);
        }

    }

    public void setList(List<T> list) {
        this.list = list;
        this.setTotalCount(list.size());
    }
}
